package online.longzipeng.back.controller;

import online.longzipeng.back.pojo.Result;
import online.longzipeng.back.pojo.entity.Team;
import online.longzipeng.back.pojo.entity.TeamMessage;
import online.longzipeng.back.pojo.param.TeamDeleteParam;
import online.longzipeng.back.pojo.param.TeamQueryParam;
import online.longzipeng.back.pojo.vo.TeamMessageVo;
import online.longzipeng.back.pojo.vo.TeamVo;
import online.longzipeng.back.service.TeamService;
import online.longzipeng.back.utils.UserUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lzp
 * 团队控制层
 */
@RestController
@RequestMapping("/team")
public class TeamController {

    @Autowired
    private TeamService teamService;

    /**
     * 团队详情
     */
    @GetMapping("/detail")
    public Result<TeamVo> detail(@RequestParam(value = "id", required = true) Long id) {
        return Result.success(teamService.detail(id));
    }


    /**
     * 获取登录用户的团队列表
     */
    @PostMapping("/list")
    public Result<List<TeamVo>> list(@RequestBody TeamQueryParam param) {
        param.setUserId(UserUtils.getUserId());
        return Result.success(teamService.list(param));
    }

    /**
     * 新增或保存团队
     */
    @PostMapping("/save")
    public Result save(@RequestBody Team team) {
        teamService.save(team);
        return Result.success();
    }

    /**
     * 加入团队
     */
    @GetMapping("/join")
    public Result join(@RequestParam(value = "teamId", required = true) Long teamId,
                       @RequestParam(value = "userId", required = true) Long userId) {
        teamService.join(teamId, userId);
        return Result.success();
    }

    /**
     * 解散团队
     */
    @GetMapping("/delete")
    public Result dissolve(@RequestParam(value = "teamId", required = true) Long teamId) {
        teamService.delete(teamId);
        return Result.success();
    }

    /**
     * 删除团队中的多个用户
     */
    @PostMapping("/delete/users")
    public Result deleteUsers(@RequestBody TeamDeleteParam param) {
        teamService.deleteUsers(param);
        return Result.success();
    }

    /**
     * 邀请加入团队
     */
    @PostMapping("/invite")
    public Result invite(@RequestBody TeamMessage teamMessage) {
        teamService.invite(teamMessage);
        return Result.success();
    }

    /**
     * 获取当前登录用户的所有团队消息
     */
    @GetMapping("/getTeamMessages")
    public Result<List<TeamMessageVo>> getTeamMessages() {
        return Result.success(teamService.getTeamMessages());
    }

    /**
     * 删除团队消息
     */
    @GetMapping("/delete/message")
    public Result deleteMessage(@RequestParam(value = "id", required = true) Long id) {
        teamService.deleteMessage(id);
        return Result.success();
    }

    /**
     * 修改团队消息的状态
     */
    @GetMapping("/handle/message")
    public Result handleMessage(@RequestParam(value = "id", required = true) Long id,@RequestParam(value = "status",required = true) Integer status) {
        teamService.handleMessage(id,status);
        return Result.success();
    }

}
